package com.hubei.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        CustomException exception = null;
        if(ex instanceof CustomException){
            exception = (CustomException) ex;
        }else {
            String errMsg = ex.getMessage();
            exception = new CustomException(errMsg);
        }
        ModelAndView mav = new ModelAndView();
        mav.addObject("errMsg",exception.getMessage());
        mav.setViewName("error");
        return mav;
    }
}
